<?php

class Action
{
    public function index(){
        $page_no = $search_filter = $is_excel = null;

        extract(URLTracker::init(array(
            'page_no' => 1,
            'search_filter' => array(),
            'is_excel' => 0,
        )));
        $order_filter['type'] = 1;
        $order_filter['is_enable'] = 1;
        $order_filter['status'] = 1;
        $order_filter['progress != '] = 15;

        $city = array();
        if (isset($search_filter['scope']) && $search_filter['scope']) {
            if($search_filter['scope'] == 'city'){
                $region_filter['depth'] = 2;
                $region_filter['parent_id'] = $search_filter['provice_id'];
                $city_list = _model('region')->getList($region_filter);
                $store_ids = array();
                $order_ids = array();
                foreach($city_list as $k => $v){
                    $city[$k]['province'] = $v['area'];
                    $province = _model('region')->getList(array('area_code' => $search_filter['provice_id']));
                    $city[$k]['province'] = $province[0]['area'];
                    $city[$k]['city'] = $v['area'];
                    $store_filter = array();
                    $store_filter['is_enable'] = 1;
                    $store_filter['city_id'] = $v['area_code'];
                    $multiple_area_filter = array();
                    $multiple_area_filter['province_code'] = $search_filter['provice_id'];
                    $regional_manager = _model('multiple_area')->getFields("user_id", $multiple_area_filter);
                    $user_realname = array();
                    foreach($regional_manager as $kkk => $vvv){
                        $user_info = _model('user')->getFields('realname',array('id' => $vvv));
                        $user_realname[] = $user_info;
                    }
                    $city[$k]['regional_manager'] = $user_realname;
                    $store_ids[$k] = _model('store')->getFields('id', $store_filter);
                }

                foreach($store_ids as $ke => $va){
                    $order_filter = array();
                    $order_id_list = array();
                    $agency_id_list = array();
                    foreach($va as $k => $v){
                        $order_filter['store_id'] = $v;
                        $order_filter['type'] = 1;
                        $order_filter['is_enable'] = 1;
                        $order_filter['status'] = 1;
                        $order_filter['progress != '] = 15;
                        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
                            $order_filter['add_time >='] = $search_filter['start_date']." 00:00:00";
                        }

                        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
                            $order_filter['add_time <='] = $search_filter['end_date']." 23:59:59";
                        }
                        $order_id_list[] = _model('order')->getFields('id', $order_filter);
                        $agency_id_list[] = _model('store')->getFields('agency_id', array('id' => $v));
                    }
                    $agency_realname = array();
                    $order_ids = $this->array_merge_rec($order_id_list);
                    $agency_ids = $this->array_merge_rec($agency_id_list);
                    $agency_ids = array_unique($agency_ids);
                    foreach($agency_ids as $kkk => $vvv){
                        if(!$vvv){
                            unset($agency_ids[$kkk]);
                            continue;
                        }
                        $agency_filter = array();
                        $agency_filter['status'] = 1;
                        $agency_filter['is_enable'] = 1;
                        $agency_filter['id'] = $vvv;
                        $user_info = _model('user')->getFields('realname',$agency_filter);
                        $agency_realname[] = $user_info;
                        $agency_id[] = $vvv;
                    }
                    $city[$ke]['agency_realname'] = $agency_realname;
                    $city[$ke]['agency_id'] = $agency_id;
                    $city[$ke]['message'] = records_center_helper::get_goods_message($order_ids);
                    $city[$ke]['number'] = $ke + 1;
                }

            }
        }

        Response::assign('article_category', records_center_config::$article_category);

        Response::assign('city', $city);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/records_center_city_list.html');
    }

    public function  array_merge_rec(&$order_id_list) {  // 参数是使用引用传递的
        // 定义一个新的数组
        $new_array = array ();
        // 遍历当前数组的所有元素
        foreach($order_id_list as $item) {
            if (is_array ($item)) {
                // 如果当前数组元素还是数组的话，就递归调用方法进行合并
                $this->array_merge_rec($item);
                // 将得到的一维数组和当前新数组合并
                $new_array = array_merge($new_array, $item);
            } else {
                // 如果当前元素不是数组，就添加元素到新数组中
                $new_array[] = $item;
            }
        }
        return $new_array;
    }
}

?>